﻿Imports System.Data.OleDb
Public Class QuoteDiscuss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)
        Dim sql As String = "INSERT INTO QuotesViewedByUsers(QuoteFKID,TwiWriter) VALUES (@f1,@f2)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()

        '----------------NOT WORKING: attempt to make sure the user's rating appears on reload or when a comment is added etc as currently a bit temperamental.
        '
        'If Not IsPostBack Then
        '    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)
        '    Dim SqlString As String = "SELECT * FROM QuoteRatings WHERE TwiWriter=@f1 and QuoteFKID=@f2"
        '    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        '    cmd.CommandType = CommandType.Text
        '    cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        '    cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        '    oleDbConn.Open()
        '    Dim dr As OleDbDataReader = cmd.ExecuteReader()
        '    If dr.HasRows Then
        '        dr.Read()
        '        If Not IsDBNull(dr("Rating")) Then lbl_Rating.Text = dr("Rating")
        '    End If
        'End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)
        conn.Open()
        Dim rmSql As String = "DELETE FROM QuoteRatings WHERE Rater=@f1 AND QuoteFKID=@f2"
        Dim rmCmd As New OleDbCommand(rmSql, conn)
        rmCmd.CommandType = CommandType.Text
        rmCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        rmCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        rmCmd.ExecuteNonQuery()

        Dim sql As String = "INSERT INTO QuoteRatings(Rating,QuoteFKID,Rater) VALUES (@f1,@f2,@f3)"
        Dim addCmd As New OleDbCommand(sql, conn)
        addCmd.CommandType = CommandType.Text
        addCmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        addCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        addCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        addCmd.ExecuteNonQuery()
        conn.Close()
        lbl_Rating.Text = Rating1.CurrentRating
    End Sub

    Protected Sub btn_quoteComment_Click(sender As Object, e As EventArgs) Handles btn_quoteComment.Click
        Dim avatarName As String = ""
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)
        Dim sql2 = "Select Avatar FROM Profile WHERE TwiWriter=@f1"
        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn)
        cmd2.CommandType = CommandType.Text
        cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
        conn.Open()
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("avatar")) Then avatarName = dr("avatar")
        End If

        Dim sql = "INSERT INTO QuoteComments (TwiWriter,Comment,QuoteFKID,AvatarFK) VALUES (@f1,@f2,@f3,@f4)"
        Dim cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_sceneComment.Text)
        cmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f4", avatarName)
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("QuoteDiscuss.aspx?ID=" & Request.Params("ID"))

    End Sub
End Class